from flask import Flask,request
import subprocess
import threading
from robot_method import send_message, get_token, send_card, send_result
from utils.decorators import de_get_token,de_log_time_exceptions
from utils.logger import log


@de_log_time_exceptions
@de_get_token
def script_execute(value):
    print(value)
    if value == "OJ_UI":
        result = subprocess.run(['pytest'],
                                cwd="D:/py/codeHome/ushannon_-oj_-uiauto",
                                capture_output=True,
                                text=True
                                )
        response =  send_result(result = result.stdout,proj_name = value)

        print(result.stdout)
        print(response.json())



app = Flask(__name__)

@app.route('/event',methods=['GET','POST'])
def event():
    log.info("开始运行")
    if request.method == 'POST':
        log.info(request.json)
        #若是对应的回调
        if request.json.get("event"):

            thread = threading.Thread(target=script_execute(request.json["event"]["action"]["value"]))
            thread.start()


        return request.json

    return  "运行成功了"



if __name__ == '__main__':
    app.run()
